package com.shashank.sony.fancytoastlib;

import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;

public class FancyToast {

    public static int SUCCESS = 1;
    public static int WARNING = 2;
    public static int ERROR = 3;
    public static int INFO = 4;
    public static int DEFAULT = 5;
    public static int CONFUSING = 6;

    public static ToastDialog makeText(Context context, String message, int duration, int type, boolean ohosIcon) {
        ToastDialog toast = new ToastDialog(context);
        toast.setDuration(duration);
        DirectionalLayout layout = (DirectionalLayout) LayoutScatter.getInstance(context).parse(ResourceTable.Layout_fancytoast_layout, null, false);
        Text l1 = (Text) layout.findComponentById(ResourceTable.Id_toast_text);
        DirectionalLayout linearLayout = (DirectionalLayout) layout.findComponentById(ResourceTable.Id_toast_type);
        Image img = (Image) layout.findComponentById(ResourceTable.Id_toast_icon);
        Image img1 = (Image) layout.findComponentById(ResourceTable.Id_imageView4);
        l1.setText(message);
        if (ohosIcon)
            img1.setVisibility(Component.VISIBLE);
        else img1.setVisibility(Component.INVISIBLE);
        switch (type) {
            case 1:
                linearLayout.setBackground(new ShapeElement(context,ResourceTable.Graphic_success_shape));
                img.setBackground(ResUtil.getVectorDrawable(context,ResourceTable.Graphic_ic_check_black_24dp));
                break;
            case 2:
                linearLayout.setBackground(new ShapeElement(context,ResourceTable.Graphic_warning_shape));
                img.setBackground(ResUtil.getVectorDrawable(context,ResourceTable.Graphic_ic_pan_tool_black_24dp));
                break;
            case 3:
                linearLayout.setBackground(new ShapeElement(context,ResourceTable.Graphic_error_shape));
                img.setBackground(ResUtil.getVectorDrawable(context,ResourceTable.Graphic_ic_clear_black_24dp));
                break;
            case 4:
                linearLayout.setBackground(new ShapeElement(context,ResourceTable.Graphic_info_shape));
                img.setBackground(ResUtil.getVectorDrawable(context,ResourceTable.Graphic_ic_info_outline_black_24dp));
                break;
            case 5:
                linearLayout.setBackground(new ShapeElement(context,ResourceTable.Graphic_default_shape));
                //img.setVisibility(Component.INVISIBLE);
                break;
            case 6:
                linearLayout.setBackground(new ShapeElement(context,ResourceTable.Graphic_confusing_shape));
                img.setBackground(ResUtil.getVectorDrawable(context,ResourceTable.Graphic_ic_refresh_black_24dp));
                break;
        }
        toast.setComponent(layout);
        toast.setAlignment(LayoutAlignment.CENTER);
        toast.setSize(ComponentContainer.LayoutConfig.MATCH_PARENT,ComponentContainer.LayoutConfig.MATCH_CONTENT);
        toast.setTransparent(true);
        return toast;
    }

    public static ToastDialog makeText(Context context, String message, int duration, int type, int ImageResource, boolean ohosIcon) {
        ToastDialog toast = new ToastDialog(context);
        toast.setDuration(duration);
        DirectionalLayout layout = (DirectionalLayout) LayoutScatter.getInstance(context).parse(ResourceTable.Layout_fancytoast_layout, null, false);
        Text l1 = (Text) layout.findComponentById(ResourceTable.Id_toast_text);
        DirectionalLayout linearLayout = (DirectionalLayout) layout.findComponentById(ResourceTable.Id_toast_type);
        Image img = (Image) layout.findComponentById(ResourceTable.Id_toast_icon);
        Image img1 = (Image) layout.findComponentById(ResourceTable.Id_imageView4);
        l1.setText(message);
        img.setBackground(ResUtil.getVectorDrawable(context,ImageResource));
        if (ohosIcon)
            img1.setVisibility(Component.VISIBLE);
        else img1.setVisibility(Component.INVISIBLE);
        switch (type) {
            case 1:
                linearLayout.setBackground(new ShapeElement(context,ResourceTable.Graphic_success_shape));
                img.setBackground(ResUtil.getVectorDrawable(context,ResourceTable.Graphic_ic_check_black_24dp));
                break;
            case 2:
                linearLayout.setBackground(new ShapeElement(context,ResourceTable.Graphic_warning_shape));
                img.setBackground(ResUtil.getVectorDrawable(context,ResourceTable.Graphic_ic_pan_tool_black_24dp));
                break;
            case 3:
                linearLayout.setBackground(new ShapeElement(context,ResourceTable.Graphic_error_shape));
                img.setBackground(ResUtil.getVectorDrawable(context,ResourceTable.Graphic_ic_clear_black_24dp));
                break;
            case 4:
                linearLayout.setBackground(new ShapeElement(context,ResourceTable.Graphic_info_shape));
                img.setBackground(ResUtil.getVectorDrawable(context,ResourceTable.Graphic_ic_info_outline_black_24dp));
                break;
            case 5:
                linearLayout.setBackground(new ShapeElement(context,ResourceTable.Graphic_default_shape));
                //img.setVisibility(Component.INVISIBLE);
                break;
            case 6:
                linearLayout.setBackground(new ShapeElement(context,ResourceTable.Graphic_confusing_shape));
                img.setBackground(ResUtil.getVectorDrawable(context,ResourceTable.Graphic_ic_refresh_black_24dp));
                break;
        }
        toast.setComponent(layout);
        toast.setAlignment(LayoutAlignment.CENTER);
        toast.setSize(ComponentContainer.LayoutConfig.MATCH_PARENT,ComponentContainer.LayoutConfig.MATCH_CONTENT);
        toast.setTransparent(true);
        return toast;
    }

}
